#include "tabview.h"

#include "lualib.h"
#include "lua.h"
#include "lauxlib.h"

#include <string.h>
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

extern lua_State* g_ufo_lua_state;

static int lua_ufoTabViewSetOrientation(lua_State *L)
{
	UFO_View view = *(UFO_View*)luaL_checkudata(L, 1, "ufo.ufoView");
	int orientation = lua_tonumber(L, 2);
	lua_pushinteger(L, ufoTabViewSetOrientation(view, orientation));
	return 1;
}

static int lua_ufoTabViewSetItemRect(lua_State *L)
{
	UFO_View view = *(UFO_View*)luaL_checkudata(L, 1, "ufo.ufoView");

	ufoRect rect = {0};
	rect.left = lua_tonumber(L, 2);
	rect.top = lua_tonumber(L, 3);
	rect.right = lua_tonumber(L, 4);
	rect.bottom = lua_tonumber(L, 5);

	lua_pushinteger(L, ufoTabViewSetItemRect(view, &rect));

	return 1;
}

static const struct luaL_Reg tabviewlib [] = {
	{"setOrientation", lua_ufoTabViewSetOrientation},
	{"setItemRect", lua_ufoTabViewSetItemRect},
	{0, 0},
};

int internal_initializationScript_tabview()
{
	if (!g_ufo_lua_state) {
		g_ufo_lua_state = luaL_newstate();
		luaL_openlibs(g_ufo_lua_state);
	}

	luaL_newmetatable(g_ufo_lua_state, "ufo.ufoView");

	lua_pushvalue(g_ufo_lua_state, -1);
	lua_setfield(g_ufo_lua_state, -2, "__index");

	luaL_register(g_ufo_lua_state, 0, tabviewlib);

	return 1;
}